# 版本更新提示

import requests
import sys
import webbrowser
import tkinter as tk
from tkinter import messagebox


def check_for_updates(current_version):
    try:
        # 构建 Gitee API 请求 URL
        url = 'https://gitee.com/api/v5/repos/Yae_Miko_Fox/YunJiIP_iteration/releases/latest'

        response = requests.get(url)
        response.raise_for_status()

        # 打印服务器返回的内容
        print("Server response:", response.text)

        # 尝试解析 JSON 响应
        try:
            data = response.json()
        except ValueError as e:
            print(f"Error parsing JSON response: {e}")
            print("Server response is not valid JSON. Response content:")
            print(response.text)
            return

        latest_version = data.get('tag_name')
        if latest_version and latest_version != current_version:
            # 显示更新提示
            update_message = f"新版本 {latest_version} 已发布，请访问以下链接下载：\n{data['assets'][0]['browser_download_url']}"
            if messagebox.askyesno("更新提示", update_message):
                webbrowser.open(data['assets'][0]['browser_download_url'])
                sys.exit(0)  # 关闭当前应用
    except requests.RequestException as e:
        print(f"Error checking for updates: {e}")


